<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品列表</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" media="all">
    <style>
        .productList-container {
            padding: 20px;
        }

        td[data-field="imgUrl"] div,
        td[data-field="negativeImgUrl"] div {
            height: 120px;
        }

        .lay-form {
            padding: 20px;
        }

        .productList-container {
            background-color: #fff;
        }

        .bar {
            display: flex;
            justify-content: flex-start;
            align-items: flex-end;
        }

        .searchInput {
            display: flex;
            align-items: flex-end;
        }

        .searchInput input {
            width: 200px;
        }

        .searchInput .layui-form-label {
            padding: 9px 0;
        }

        .bar .layui-form-label {
            padding: 0;
            padding-bottom: 4px;
            text-align: left;
        }

        .bar .layui-inline {
            margin-right: 20px;
        }

        .layui-table img {
            max-width: 200px;
            object-fit: contain;
        }

        .layui-table-cell {
            height: auto;
        }
    </style>
</head>

<body>
<div class="productList-container">

    <!-- 搜索 -->
    <div class="searchInput">
        <form class="layui-form" action="" lay-filter="resets">
            <div class="layui-inline">
                <label class="layui-form-label">商品名称：</label>
                <input class="layui-input" name="goodsName" placeholder="请输入商品名称" autocomplete="off">
            </div>
            <div class="layui-inline">
                <div class="layui-form-item" id="status" style="margin-bottom:0">
                    <label class="layui-form-label" style="text-align:right; font-size:10px;padding: 9px 0;">商品类型：</label>
                    <div class="layui-input-block" style="margin-left: 80px; margin-top: 0px;">
                        <select name="vip" lay-filter="select">
                            <option value="2">全部</option>
                            <option value="1">会员商品</option>
                            <option value="0">普通商品</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品分类：</label>
                <div class="layui-input-inline">
                    <select name="categoryId" lay-search="" id="productClass">
                        <option value="">请选择商品分类</option>

                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">创建日期：</label>
                <input type="text" class="layui-input" id="test1" name="createTimeStart" placeholder="请输入创建日期" autocomplete="off">
            </div>
            <button class="layui-btn" data-type="reload" lay-submit="" lay-filter="searchForm">搜索</button>
            <button class="layui-btn layui-btn-primary" data-type="reload" lay-submit="" lay-filter="reset">重置</button>
        </form>
    </div>

    <table lay-filter="productListTable" class="layui-hide" id="productListTable"></table>
    <div style="display: none;" id="delLayer">

        <form class="layui-form" action="" lay-filter="example">
            <div style="padding:15px;">确认删除选中的商品数据？移除后商品不可恢复</div>
            <input type="hidden" id = "delId" name="id"/>
            <div style="padding-left:136px;">
                <button class="closeLayer layui-btn layui-btn-primary">取消</button>
                <button type="submit" class="layui-btn"  lay-submit="" lay-filter="affirmLayer" >确认</button>
            </div>
        </form>
    </div>
</div>

</body>

<!-- 添加 -->
<script type="text/html" id="toolbarDemo">
    <div class="bar">
        <div class="layui-btn-container" style="margin-bottom:-5px;">
            <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add">添加</button>
            <!-- <button class="layui-btn layui-btn-sm" lay-event="success">上架</button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="fail">下架</button> -->
        </div>

        <!-- 筛选 -->
        <!-- <div class="layui-form-item" id="status" style="margin-bottom:0">
        <label class="layui-form-label" style="width: 60px; text-align:right; margin-top:10px">推荐状态：</label>
        <div class="layui-input-block" style="margin-left: 70px">
          <select name="stop" lay-filter="select">
            <option value="">全部</option>
            <option value="0">推荐</option>
            <option value="1">未推荐</option>
          </select>
        </div>
    </div> -->
    </div>
</script>

<!-- 操作 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>
</script>

<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.all.min.js"></script>
<script src="../utils.js"></script>
<script>

    layui.use(['table', 'upload', 'form', 'jquery', "laydate"], function() {
        var table = layui.table,
            upload = layui.upload,
            form = layui.form,
            laydate = layui.laydate,
            $ = layui.jquery;

        table.render({
            elem: '#productListTable',
            toolbar: "#toolbarDemo",
            url: window.baseUrl + '/goods/getGoodsList',
            method: 'post',
            page: false,
            contentType: 'application/json',
            headers: {
                Authorization: localStorage.token
            },
            response: {
                statusCode: 200 //规定成功的状态码，默认：0
            },
            cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,
            parseData: function(res) { //res 即为原始返回的数据
                return {
                    "code": 200,
                    "count": res.data.recordsTotal, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            request: {
                pageName: 'start' //页码的参数名称，默认：page
                ,
                limitName: 'length' //每页数据量的参数名，默认：limit
            },
            cols: [
                [{
                    type: 'checkbox'
                }, {
                    field: 'id',
                    width: 80,
                    title: 'ID',
                    sort: true
                }, {
                    field: 'fullName',
                    title: '商品图片',
                    templet: function(res) {
                        let a = res.skuImgUrl.split(",");
                        return "<img style='width:100px; height:100px; object-fit: contain;' class='viewImg' src='" + a[0] + "'/>"
                    }
                }, {
                    field: 'goodsName',
                    title: '商品名称'
                }, {
                    field: 'categoryName',
                    title: '商品分类'
                }
                    // , { field: "recommend", title: '价格', width: 140 }
                    , {
                    title: '推荐',
                    width: 100,
                    templet: function(res) {
                        return '<input type="checkbox" ' + (res.recommend == 1 ? 'checked=""' : "") + '" lay-filter="switchTest" lay-skin="switch" lay-text="推荐|未推荐" value="' + res.recommend + '" data-id="' + res.id + '"/>'

                    }
                },{
                    title: '会员商品',
                    width: 100,
                    templet: function(res) {
                        return '<input type="checkbox" ' + (res.vip == 1 ? 'checked=""' : "") + '" lay-filter="switchVip" lay-skin="switch" lay-text="会员|非会员" value="' + res.vip + '" data-id="' + res.id + '"/>'

                    }
                }, {
                    title: '上下架状态',
                    width: 100,
                    templet: function(res) {
                        return "<span>" + (res.status && res.status == 1 ? "上架" : "下架") + "</span>"
                    }
                },
                    {
                        field: "skuPrice",
                        title: '价格(元)',
                        width: 80
                    },{
                    field: "skuSaleNum",
                    title: '销量',
                    width: 80
                }, {
                    field: "skuNum",
                    title: '库存',
                    width: 80
                }, {
                    title: '商品类型',
                    width: 120,
                    templet: function(res) {
                        return "<span>" + (res.vip ? "会员商品" : "普通商品") + (res.integralStatus ? "<br/>积分商品" : '') + "</span>"
                    }
                }, {
                    field: "createTime",
                    title: '添加时间',
                    width: 160
                }, {
                    title: '操作',
                    toolbar: '#barDemo',
                    width: 180
                }
                ]
            ],
            page: true,
            done: function() {
                // 放大图片
                $(".viewImg").on("click", function() {
                    let url = $(this).attr("src");
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        area: ['auto'],
                        skin: 'layui-layer-nobg', //没有背景色
                        shadeClose: true,
                        content: "<img src=" + url + " />"
                    });
                })
            }
        });

        laydate.render({
            elem: '#test1',
            range: true
        });

        // 获取分类

        ajax({
            path: "/goods/getCategoryList?length=99999999",
            type: "GET",
            success: function(res) {
                console.log(res.data);
                res.data.forEach(el => {
                    $('#productClass').append(`<option value="${el.id}">${el.categoryName}</option>`)
            })

                form.render();
            }
        }, $)

        // 是否推荐
        form.on("switch(switchTest)", function(e) {
            let id = $(e.elem).attr('data-id');
            // 删除
            if(e.value == 1) {
                ajax({
                    path: "/goods/saveOrUpdateGoods",
                    data: {
                        id: id,
                        interfaceType: 1,
                        recommend: 0
                    },
                    success: function(res) {
                        table.reload("productListTable");
                    }
                }, $)
            }
            // 添加
            else {
                ajax({
                    path: "/goods/saveOrUpdateGoods",
                    data: {
                        id: id,
                        interfaceType: 1,
                        recommend: 1
                    },
                    success: function(res) {
                        table.reload("productListTable");
                    }
                }, $)
            }
        })

        form.on("switch(switchVip)", function(e) {
            let id = $(e.elem).attr('data-id');
            // 删除
            if(e.value == 1) {
                ajax({
                    path: "/goods/saveOrUpdateGoods",
                    data: {
                        id: id,
                        interfaceType: 2,
                        vip: 0
                    },
                    success: function(res) {
                        table.reload("productListTable");
                    }
                }, $)
            }
            // 添加
            else {
                ajax({
                    path: "/goods/saveOrUpdateGoods",
                    data: {
                        id: id,
                        interfaceType: 2,
                        vip: 1
                    },
                    success: function(res) {
                        table.reload("productListTable");
                    }
                }, $)
            }
        })


        //工具栏事件
        table.on('toolbar(productListTable)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event) {
                case "add":
                    window.location.href = "../addProduct/addProduct.html";
                    break;
                case "del":
                    if(checkStatus.data.length == 0) {
                        layer.msg('请选择需要删除的商品');
                        return false;
                    }
                    layer.open({
                        title:'删除确认',
                        type: 1,
                        offset: 'auto',
                        id: 'layerDemoauto',
                        content: $("#delLayer"),
                        btnAlign: 'c',
                        area: ['300px', '150px'],
                        shade: 0,

                    });
                    break;
            };
        });
        form.on("submit(affirmLayer)", function(el) {
            ajax({
                path: "/goods/deleteGoods?id="+el.field.id,
                success: function (res) {
                    layer.closeAll();
                    layer.msg("删除成功");
                }
            }, $)
            layer.closeAll();
            return false;
        })
        $(".closeLayer").on("click",function(){
            layer.closeAll();
        })
        // 搜索
        form.on("submit(searchForm)", function(e) {

            for(let key in e.field) {
                if(!e.field[key]) {
                    delete e.field[key]
                }
            }

            if(e.field.createTimeStart) {
                var a = e.field.createTimeStart.split(" - ")
                e.field.createTimeStart = new Date(a[0]).getTime();
                e.field.createTimeEnd = new Date(a[1]).getTime();
            }

            table.reload('productListTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                },
                where: e.field
            });

            return false;
        })

        // 搜索
        form.on("submit(reset)", function(e) {

            table.reload('productListTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                },
                where: null
            });

            form.val("resets", {
                createTimeStart: "",
                goodsName: "",
                spuNo: "",
                categoryId: ""
            });

            return false;
        })


        // 编辑和删除
        table.on("tool(productListTable)", function(data) {
            console.log(data);
            switch(data.event) {
                case "edit":
                    window.location.href = "../addProduct/addProduct.html?spuId=" + data.data.id;
                    break;
                case "delete":
                    layer.open({
                        title:'删除确认',
                        type: 1,
                        offset: 'auto',
                        id: 'layerDemoauto',
                        content: $("#delLayer"),
                        btnAlign: 'c',
                        area: ['300px', '200px'],
                        shade: 0,

                    });
                    form.val('example',{
                        id: data.data.id
                    })
                    break;
            }
        })
    });
</script>

</html>